<div id="main">
    <div class="search">
        <p><h4>搜索：</h4></p>
        <ul class="nav nav-pills">
            <form action="index" method="post">
            <input type="hidden" name="page" value="<?php echo $page;?>" />
            <input type="hidden" name="total" value="<?php echo $total;?>" />
            <input type="hidden" name="limit" value="<?php echo $limit;?>" />
            <li><label> 日期：<input type="text" class="_input" id="start_datepicker" value="<?php echo $start;?>" name="start"></label></li>
<!--            <li><label><select name="product"><option value="-1">--请选择产品--</option></select></label></li>-->
            <li><input type="submit" class="_submit" value="搜索"/></li>
            </form>
        </ul>
    </div>
    <div class="panel panel-default" style="margin-top:20px;">
	<?php if(strtotime($start) > time() - 7 * 24 * 3600):?>
	<div class="download">
		<a class="_btn theme_blue" style="float:right;"target="_blank" href="/export/url/<?php echo $start;?>.tar.gz">下载</a>
	</div>
	<?php endif;?>
        <div class="panel-body">
            <table class="table table-hover">
                <thead>
                <tr>
                    <th width="150px">日期</th>
                    <th width="120px">产品</th>
                    <th width="220px">客户端</th>
                    <th width="120px">时长</th>
                    <th width="200px">URL</th>
		</tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>
        <div class="page" id="page"></div>
    </div>
</div>
<script src="/static/pager/pager.js"></script>
<script>
    $(function(){ 
        $("#start_datepicker").datepicker({
            dateFormat: "yy-mm-dd",
            maxDate: new Date(),
            onselect:function(dateText,inst){
                $("#end_datepicker").datepicker("option","minDate",dateText);  
            }
        });


        var search_product = '<?php echo $product;?>';
        var page = $("input[name=page]").val();
        var total = $("input[name=total]").val();
        var limit = $("input[name=limit]").val();
        var date = $("#start_datepicker").val();

        var product_data;
        getProduct();

        $("#start_datepicker").change(function() {
            date = $("#start_datepicker").val();
            //getProduct();
        });
        function getProduct() {
            //加载产品
            $.ajax({
                url:'/index.php/Admin/url/all_channel/' + date,
                dataType:'json',
                async:false,
                success: function (data) {
                    product_data = data;
                }
            });
            showChannel();
        }

        function showChannel() {
            if(typeof product_data != 'object' || product_data.length <= 0) {
                return false;
            }
            var content = '<option value="-1">--请选择产品--</option>';
            var channels_arr = [];
            for(var key in product_data) {
                var selected = '';
		        var val = key.split('_')[1];
                if(val == search_product) {
                    selected = 'selected = "selected"';
                }
                content += '<option value="' + val + '" ' + selected + '>' + val + '</option>';

            }
            $("select[name=product]").html(content);
        }

        get_content(page);
        var pageObj = new Pager();
        pageObj.init({total:total,currentPage:page,limit:limit,showNum:20});
        pageObj.showPages();

        $('a.g_page').click(function() {
            var ele = $(this);
            if(ele.hasClass('g_active')) {
                return false;
            }
            if(ele.hasClass('g_prev')) {
                page = pageObj.prev();
            } else if(ele.hasClass('g_next')) {
                page = pageObj.next();
            } else {
                page = ele.text();
                page = pageObj.click(page);
            }
            get_content(page);
        });
//        //搜索
//        $("select[name=product]").change(function () {
//            product = $(this).val();
//            $.ajax({
//                url:'/Admin/Url/ajaxUrl/' + date + '/' + product,
//                dataType:'json',
//                data:{},
//                type:'post',
//                async:false,
//                success: function (data) {
//                    if(typeof data != 'object' || data.list.length == 0) {
//                        $("tbody").html("<tr><td colspan='5'>没有数据</td></tr>")
//                        return false;
//                    }
//                    show_list(data.list);
//                }
//            });
//        });
//
//        $("#start_datepicker").change(function () {
//            date = $(this).val();
//            $.ajax({
//                url:'/Admin/Url/ajaxUrl/' + date + '/' + product,
//                dataType:'json',
//                data:{},
//                type:'post',
//                async:false,
//                success: function (data) {
//                    if(typeof data != 'object' || data.list.length == 0) {
//                        $("tbody").html("<tr><td colspan='5'>没有数据</td></tr>")
//                        return false;
//                    }
//                    show_list(data.list);
//                }
//            });
//        });
        function get_content() {
            $.ajax({
                url:'/index.php/Admin/Url/ajaxUrl/' + date + '/' + search_product,
                dataType:'json',
                data:{page:page},
                type:'post',
                async:false,
                success: function (data) {
                    if(typeof data != 'object' || data.list.length == 0) {
                        $("tbody").html("<tr><td colspan='5'>没有数据</td></tr>")
                        return false;
                    }
                    show_list(data.list);
                }
            });
        }

        function show_list(list) {
            var content = '';
            for(var i = 0; i < list.length; i ++) {
                content += '<tr>';
                content += '<td class="center">'+date+'</td>';
                content += '<td class="center">'+list[i]['productid']+'</td>';
                content += '<td class="center">'+list[i]['uuid']+'</td>';
                content += '<td class="center">'+list[i]['time']+'</td>';
                content += '<td>'+list[i]['domain']+'</td>';
                content += '</tr>';
            }
            $("tbody").html(content);
        }
    });    
</script>
